package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVO> query(VehicleQuery vehicleQuery) {
        List<VehicleVO> list = vehicleMapper.query(vehicleQuery);
        return list;
    }

    @Override
    public void save(VehicleSaveParam vehicleSaveParam) {
        Vehicle vehicle = new Vehicle() ;
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        System.out.println(vehicle);
        if (vehicle.getId() == null)
        {
            System.out.println("新增");
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceId(null);
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.insert(vehicle);
        }else {
            System.out.println("修改");
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }



        vehicleMapper.insert(vehicle);
    }

    @Override
    public void del(Long id) {
        vehicleMapper.del(id);
    }

    @Override
    public void unbind(Long vehicleId) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(null);
        vehicle.setGeofenceBindStatus("0");
        vehicleMapper.unbind(vehicle);
    }

    @Override
    public List<VehicleVO> selectUnBindVehicle() {
        return vehicleMapper.selectUnBindVehicle();
    }

    @Override
    public void bind(Long vehicleid, Long geoid) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleid);
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geoid);
        vehicleMapper.bind(vehicle);
    }
}
